<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="审核项" align="center" prop="name">
      </el-table-column>
      <el-table-column label="是否需要审核" align="center">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.id"
            @change="updateHandle($event, scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { disciplineGroup_onOffStatus } from "@/api/disciplineGroup/disciplineGroup";
import {
  getPost_school,
  updatePost_school,
} from "@/api/schoolOrganization/schoolInfo";
export default {
  name: "auditConfiguration",
  data() {
    return {
      // 表格数据
      tableData: [],
      info: {},
      loading: false,
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    /** 获取学校信息 */
    async getInfo() {
      this.loading = true;
      let res = await getPost_school();
      const { code, data } = res;
      if (code == 200) {
        this.loading = false;
        this.info = data;
        this.tableData = [
          {
            name: "学校审核课程",
            key: "courseNeedCheckSchool",
            id: data.courseNeedCheckSchool,
            type: "course",
          },
          {
            name: "分院审核课程",
            key: "courseNeedCheckBranch",
            id: data.courseNeedCheckBranch,
            type: "course",
          },
          {
            name: "学科专业组审核课程",
            key: "courseNeedCheckDiscipline",
            id: data.courseNeedCheckDiscipline,
            type: "course",
          },
          {
            name: "学校审核工作站",
            key: "workstationNeedCheckSchool",
            id: data.workstationNeedCheckSchool,
            type: "workstation",
          },
          {
            name: "分院审核工作站",
            key: "workstationNeedCheckBranch",
            id: data.workstationNeedCheckBranch,
            type: "workstation",
          },
        ];
      }
    },
    async updateHandle(e, item) {
      // 要关闭最后一个 课程或者 工作站才会提示
      if (
        this.tableData
          .filter((data) => data.type == item.type)
          .every((data) => data.id == 0)
      ) {
        if (e != 1) {
          let txt =
            item.name.indexOf("工作站") > -1
              ? "关闭后工作站提交审核后将自动通过，同时审核中工作站将自动通过审核，是否确认关闭？"
              : "关闭后课程提交审核后将自动通过，同时审核中课程将自动通过审核，是否确认关闭？";
          this.$confirm(txt, "关闭确认", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            closeOnClickModal: false, // 点击遮罩层不关闭
            closeOnPressEscape: false, // 按 Esc 键不关闭
            showClose: false, // 隐藏右上角的关闭按钮
          })
            .then(async () => {
              try {
                this.info[item.key] = item.id;
                let res = await updatePost_school(this.info);
                console.log(res);
                const { code } = res;

                if (code == 200) {
                  this.$modal.msgSuccess("配置成功");
                  this.getInfo();
                }
              } catch (error) {
                console.log(error);
                item.id = e == 1 ? 0 : 1;
              }
            })
            .catch((e) => {
              // 已取消
              this.$modal.msgInfo("已取消");
              item.id = e == 1 ? 0 : 1;
            });
        }
        // 开启学科专业组 需要去判断 总的那里是否开启
        else {
          try {
            if (item.key == "courseNeedCheckDiscipline") {
              let switchRes = await disciplineGroup_onOffStatus();
              if (switchRes.code == 200) {
                if (switchRes.data.onOffStatus == 0) {
                  this.$modal.msgWarning(
                    "请先前往学校组织管理中开启学科专业组！"
                  );
                  item.id = 0;
                  return false;
                }
              }
            }
            this.info[item.key] = item.id;
            let res = await updatePost_school(this.info);
            console.log(res);
            const { code } = res;

            if (code == 200) {
              this.$modal.msgSuccess("配置成功");
              this.getInfo();
            }
          } catch (error) {
            console.log(error);
            item.id = e == 1 ? 0 : 1;
          }
        }
      } else {
        try {
          this.info[item.key] = item.id;
          let res = await updatePost_school(this.info);
          console.log(res);
          const { code } = res;

          if (code == 200) {
            this.$modal.msgSuccess("配置成功");
            this.getInfo();
          }
        } catch (error) {
          console.log(error);
          item.id = e == 1 ? 0 : 1;
        }
      }
    },
  },
};
</script>

<style scoped lang="scss">

</style>
